package com.daka.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.daka.dao.UserMapper;
import com.daka.entity.User;
import com.daka.enumerate.CommonStatus;
import com.daka.enumerate.StatusCode;
import com.daka.service.UserService;
import com.daka.tools.MD5Util;
import com.daka.tools.SendSMS;
import com.daka.vo.request.UserRegisterRequestVo;
import com.daka.vo.response.DataResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author beliveli on 2016/3/31 0031.
 */
@Service("userService")
@SuppressWarnings("unused")
public class UserServiceImpl implements UserService {

    @Resource(name = "userMapper")
    private UserMapper userMapper;

    @Resource(name = "redisClient")
    private RedisClient redisClient;

    public User selectByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    public User selectById(int manageId) {
        return userMapper.selectById(manageId);
    }

    public int updatePasswordById(User user) {
        return userMapper.updatePasswordById(user);
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public List<User> selectByParam(String roles,String key) {
        return userMapper.selectByParam(roles, key);
    }

    public DataResponseVo<User> login(User userParam) {
        User user = userMapper.selectByPhone(userParam.getPhone());

        if (user == null) {
            return new DataResponseVo<User>(StatusCode.NOT_LOGIN.getStatusCode(), "user not exists");
        }

        if (user.getPassword().equalsIgnoreCase(MD5Util.MD5(userParam.getPassword()))) {
            return new DataResponseVo<User>(StatusCode.SUCCESS.getStatusCode(), "", user);
        } else {
            return new DataResponseVo<User>(StatusCode.SELECT_ITEM_NOT_EXISTS.getStatusCode(), "Username password do not match");
        }
    }

    public User saveUser(User userParam) {

        int resultCount;
        User user;
        if (userParam.getManageId() == 0) {
            userParam.setCreateTime(new Timestamp(new Date().getTime()));
            userParam.setStatus(CommonStatus.DISABLED.getStatusCode());
            userParam.setPassword(MD5Util.MD5(userParam.getPassword()));

            resultCount = userMapper.saveUser(userParam);
        } else {
            resultCount = userMapper.updateById(userParam);
        }

        if (resultCount == 0) {
            return null;
        }
        return userParam;
    }

    public DataResponseVo<User> updateUser(User userParam) {
        User user = selectById(userParam.getManageId());
        user.setManageName(userParam.getManageName());
        user.setImage(userParam.getImage());
        user.setNickName(userParam.getNickName());
        user.setPhone(userParam.getPhone());
        user.setRoles(userParam.getRoles());

        if (!StringUtils.isEmpty(userParam.getPassword())) {
            user.setPassword(MD5Util.MD5(userParam.getPassword()));
        }

        int resultCount = userMapper.updateById(user);

        if (resultCount == 0) {
            return new DataResponseVo<User>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<User>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), user);
    }

    public String sendVerifierCode(String mobile){
        String randCode = "";
        try
        {
            //发送手机验证码
            randCode = get4Rand();
            String value = SendSMS.sendMsgFromTemplate(mobile.trim(), randCode);
            System.out.println(value);
            redisClient.set("resetPassword",mobile, randCode);
            redisClient.expire(mobile, 60);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return randCode;
    }

    public Boolean checkVerifierCode(String mobile,String code){
        try
        {
            String redisCode = (String)redisClient.get("resetPassword", mobile,String.class);
            if(redisCode != null && code.equals(redisCode)){
                redisClient.remove("resetPassword", mobile);
                return true;
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    private static String get4Rand()
    {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (9999 - 1000 + 1) + 1000);
    }

}
